{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "\n",
    "# Mult Source RAG agent - Export Import Agent\n"
   ],
   "metadata": {
    "id": "4Ws460ggG6GL"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "This project implements a **multi-source Retrieval Augmented Generation (RAG) agent** designed to assist users with **export and import guidance**. It integrates **LanceDB** for efficient document retrieval and **Tavily** for real-time external searches. The system helps users navigate **export/import policies, product classifications, compliance guidelines, and industry updates**."
   ],
   "metadata": {
    "id": "B2wi0Gd4kKAw"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "**Install dependencies**"
   ],
   "metadata": {
    "id": "NsHUymGz8heI"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "!pip install langchain langchain-openai langgraph langchain_community\n",
    "!pip install pypdf\n",
    "!pip install lancedb"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "THmMrnQXY_oN",
    "outputId": "694862ac-e1b6-4d07-d549-d017b2563a38"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Requirement already satisfied: langchain in /usr/local/lib/python3.11/dist-packages (0.3.19)\n",
      "Requirement already satisfied: langchain-openai in /usr/local/lib/python3.11/dist-packages (0.3.7)\n",
      "Requirement already satisfied: langgraph in /usr/local/lib/python3.11/dist-packages (0.2.74)\n",
      "Requirement already satisfied: langchain_community in /usr/local/lib/python3.11/dist-packages (0.3.18)\n",
      "Requirement already satisfied: langchain-core<1.0.0,>=0.3.35 in /usr/local/lib/python3.11/dist-packages (from langchain) (0.3.40)\n",
      "Requirement already satisfied: langchain-text-splitters<1.0.0,>=0.3.6 in /usr/local/lib/python3.11/dist-packages (from langchain) (0.3.6)\n",
      "Requirement already satisfied: langsmith<0.4,>=0.1.17 in /usr/local/lib/python3.11/dist-packages (from langchain) (0.3.8)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /usr/local/lib/python3.11/dist-packages (from langchain) (2.10.6)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.11/dist-packages (from langchain) (2.0.38)\n",
      "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.11/dist-packages (from langchain) (2.32.3)\n",
      "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.11/dist-packages (from langchain) (6.0.2)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.11/dist-packages (from langchain) (3.11.12)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<10,>=8.1.0 in /usr/local/lib/python3.11/dist-packages (from langchain) (9.0.0)\n",
      "Requirement already satisfied: numpy<2,>=1.26.4 in /usr/local/lib/python3.11/dist-packages (from langchain) (1.26.4)\n",
      "Requirement already satisfied: openai<2.0.0,>=1.58.1 in /usr/local/lib/python3.11/dist-packages (from langchain-openai) (1.61.1)\n",
      "Requirement already satisfied: tiktoken<1,>=0.7 in /usr/local/lib/python3.11/dist-packages (from langchain-openai) (0.9.0)\n",
      "Requirement already satisfied: langgraph-checkpoint<3.0.0,>=2.0.10 in /usr/local/lib/python3.11/dist-packages (from langgraph) (2.0.16)\n",
      "Requirement already satisfied: langgraph-sdk<0.2.0,>=0.1.42 in /usr/local/lib/python3.11/dist-packages (from langgraph) (0.1.53)\n",
      "Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in /usr/local/lib/python3.11/dist-packages (from langchain_community) (0.6.7)\n",
      "Requirement already satisfied: pydantic-settings<3.0.0,>=2.4.0 in /usr/local/lib/python3.11/dist-packages (from langchain_community) (2.8.0)\n",
      "Requirement already satisfied: httpx-sse<1.0.0,>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from langchain_community) (0.4.0)\n",
      "Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (2.4.6)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.2)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (25.1.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.5.0)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.1.0)\n",
      "Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (0.2.1)\n",
      "Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.18.3)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /usr/local/lib/python3.11/dist-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (3.26.1)\n",
      "Requirement already satisfied: typing-inspect<1,>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (0.9.0)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.35->langchain) (1.33)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.35->langchain) (24.2)\n",
      "Requirement already satisfied: typing-extensions>=4.7 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.35->langchain) (4.12.2)\n",
      "Requirement already satisfied: msgpack<2.0.0,>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from langgraph-checkpoint<3.0.0,>=2.0.10->langgraph) (1.1.0)\n",
      "Requirement already satisfied: httpx>=0.25.2 in /usr/local/lib/python3.11/dist-packages (from langgraph-sdk<0.2.0,>=0.1.42->langgraph) (0.28.1)\n",
      "Requirement already satisfied: orjson>=3.10.1 in /usr/local/lib/python3.11/dist-packages (from langgraph-sdk<0.2.0,>=0.1.42->langgraph) (3.10.15)\n",
      "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from langsmith<0.4,>=0.1.17->langchain) (1.0.0)\n",
      "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from langsmith<0.4,>=0.1.17->langchain) (0.23.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.58.1->langchain-openai) (3.7.1)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.58.1->langchain-openai) (1.9.0)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.58.1->langchain-openai) (0.8.2)\n",
      "Requirement already satisfied: sniffio in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.58.1->langchain-openai) (1.3.1)\n",
      "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.58.1->langchain-openai) (4.67.1)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.27.2 in /usr/local/lib/python3.11/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain) (2.27.2)\n",
      "Requirement already satisfied: python-dotenv>=0.21.0 in /usr/local/lib/python3.11/dist-packages (from pydantic-settings<3.0.0,>=2.4.0->langchain_community) (1.0.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain) (3.4.1)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain) (3.10)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain) (2.3.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain) (2025.1.31)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.11/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (3.1.1)\n",
      "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.11/dist-packages (from tiktoken<1,>=0.7->langchain-openai) (2024.11.6)\n",
      "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (1.0.7)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (0.14.0)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.11/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.35->langchain) (3.0.0)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in /usr/local/lib/python3.11/dist-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain_community) (1.0.0)\n",
      "Requirement already satisfied: pypdf in /usr/local/lib/python3.11/dist-packages (5.3.0)\n",
      "Requirement already satisfied: lancedb in /usr/local/lib/python3.11/dist-packages (0.19.0)\n",
      "Requirement already satisfied: deprecation in /usr/local/lib/python3.11/dist-packages (from lancedb) (2.1.0)\n",
      "Requirement already satisfied: pylance==0.23.0 in /usr/local/lib/python3.11/dist-packages (from lancedb) (0.23.0)\n",
      "Requirement already satisfied: tqdm>=4.27.0 in /usr/local/lib/python3.11/dist-packages (from lancedb) (4.67.1)\n",
      "Requirement already satisfied: pydantic>=1.10 in /usr/local/lib/python3.11/dist-packages (from lancedb) (2.10.6)\n",
      "Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from lancedb) (24.2)\n",
      "Requirement already satisfied: overrides>=0.7 in /usr/local/lib/python3.11/dist-packages (from lancedb) (7.7.0)\n",
      "Requirement already satisfied: pyarrow>=14 in /usr/local/lib/python3.11/dist-packages (from pylance==0.23.0->lancedb) (17.0.0)\n",
      "Requirement already satisfied: numpy>=1.22 in /usr/local/lib/python3.11/dist-packages (from pylance==0.23.0->lancedb) (1.26.4)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic>=1.10->lancedb) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.27.2 in /usr/local/lib/python3.11/dist-packages (from pydantic>=1.10->lancedb) (2.27.2)\n",
      "Requirement already satisfied: typing-extensions>=4.12.2 in /usr/local/lib/python3.11/dist-packages (from pydantic>=1.10->lancedb) (4.12.2)\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Pass your OpenAI API key and Tavily API key for accessing web search results.\n",
    "\n"
   ],
   "metadata": {
    "id": "OkSC9wvy3mHD"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-pr-\"\n",
    "os.environ[\"TAVILY_API_KEY\"] = \"tvly-\""
   ],
   "metadata": {
    "id": "dwn5lf_rTKQM"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "**Configure Directories**:  \n",
    "\n",
    "\n",
    "We are creating a real-world case study on export-import business compliance, following different guidelines that companies must adhere to. This example will incorporate export policy tools, import policy regulations, and basic guidance.  \n",
    "\n",
    "To simplify the process, we are using data from the **DGFT Import-Export Policy** available at [DGFT Website](https://www.dgft.gov.in/CP/?opt=itchs-import-export). You can download the necessary data from the provided link.  \n",
    "\n",
    "Feel free to utilize as many resources as needed to align with the requirements.\n",
    "\n",
    "\n",
    " Define paths for your PDF directories containing documents related to export/import guides, export policies, and import policies.\n"
   ],
   "metadata": {
    "id": "3BPNV1rTHLMB"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# For demo purpose we are directly downloading data using below links"
   ],
   "metadata": {
    "id": "oQAiU46Ym7p3"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "import os\n",
    "import requests\n",
    "\n",
    "# Define folders and their respective files\n",
    "folders_and_files = {\n",
    "    \"export_import_policies/how_to_export\": {\n",
    "        \"HTE_basics.pdf\": \"https://raw.githubusercontent.com/akashAD98/dummy_data/main/Export_import_agent/how_to_export/HTE_basics.pdf\"\n",
    "    },\n",
    "    \"export_import_policies/export_product_policy\": {\n",
    "        \"export_of_organic_chem.pdf\": \"https://raw.githubusercontent.com/akashAD98/dummy_data/main/Export_import_agent/export_policy/export_of_organic_chem.pdf\",\n",
    "        \"export_of_spices_nuts.pdf\": \"https://raw.githubusercontent.com/akashAD98/dummy_data/main/Export_import_agent/export_policy/export_of_spices_nuts.pdf\",\n",
    "        \"export_of_sugar_n_product.pdf\": \"https://raw.githubusercontent.com/akashAD98/dummy_data/main/Export_import_agent/export_policy/export_of_sugar_n_prdcut.pdf\",\n",
    "        \"export_of_veggine_nuts_cashew.pdf\": \"https://raw.githubusercontent.com/akashAD98/dummy_data/main/Export_import_agent/export_policy/export_of_veggine_nuts_cashwe.pdf\",\n",
    "    },\n",
    "    \"export_import_policies/import_product_policy\": {\n",
    "        \"import_of_veggie2.pdf\": \"https://raw.githubusercontent.com/akashAD98/dummy_data/main/Export_import_agent/import_policy/import_of_veggie2.pdf\",\n",
    "        \"vegetable_nuts_import.pdf\": \"https://raw.githubusercontent.com/akashAD98/dummy_data/main/Export_import_agent/import_policy/vegitable_nuts_import.pdf\",\n",
    "    },\n",
    "}\n",
    "\n",
    "\n",
    "# Function to create folder if not exists and download files\n",
    "def download_files():\n",
    "    for folder, files in folders_and_files.items():\n",
    "        os.makedirs(folder, exist_ok=True)  # Create folder if not exists\n",
    "        for file_name, url in files.items():\n",
    "            file_path = os.path.join(folder, file_name)\n",
    "            if not os.path.exists(file_path):  # Avoid redownloading\n",
    "                response = requests.get(url)\n",
    "                if response.status_code == 200:\n",
    "                    with open(file_path, \"wb\") as file:\n",
    "                        file.write(response.content)\n",
    "                    print(f\"Downloaded: {file_name} → {folder}\")\n",
    "                else:\n",
    "                    print(f\"Failed to download: {file_name}\")\n",
    "            else:\n",
    "                print(f\"Already exists: {file_name} → {folder}\")\n",
    "\n",
    "\n",
    "# Run the function\n",
    "download_files()\n",
    "print(\"All files downloaded successfully! ✅\")"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "T_hP37msIkOv",
    "outputId": "de28c62e-4ad4-45b1-eb31-faf9f34e6b09"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Downloaded: HTE_basics.pdf → export_import_policies/how_to_export\n",
      "Downloaded: export_of_organic_chem.pdf → export_import_policies/export_product_policy\n",
      "Downloaded: export_of_spices_nuts.pdf → export_import_policies/export_product_policy\n",
      "Downloaded: export_of_sugar_n_product.pdf → export_import_policies/export_product_policy\n",
      "Downloaded: export_of_veggine_nuts_cashew.pdf → export_import_policies/export_product_policy\n",
      "Downloaded: import_of_veggie2.pdf → export_import_policies/import_product_policy\n",
      "Downloaded: vegetable_nuts_import.pdf → export_import_policies/import_product_policy\n",
      "All files downloaded successfully! ✅\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "We are loading and processing documents using **LanceDB** for retrieval and **OpenAIEmbeddings** for querying.\n"
   ],
   "metadata": {
    "id": "buLUmGgjl2XB"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "We are utilizing four different tools to facilitate export and import operations:  \n",
    "\n",
    "### **Tools:**  \n",
    "- **`guide_on_how_to_export`** – Retrieves guides on export/import procedures.  \n",
    "- **`export_policy`** – Provides export policies and related documentation.  \n",
    "- **`import_policy`** – Fetches import policies and HS codes.  \n",
    "- **`news_export`** – Delivers real-time updates on export/import news.  \n",
    "\n",
    "These tools are integrated using a React agent to ensure seamless functionality."
   ],
   "metadata": {
    "id": "vOs4nsRKnEnO"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "from langchain_community.document_loaders import PyPDFDirectoryLoader\n",
    "from langchain_community.vectorstores import LanceDB\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "from langchain_core.tools import tool\n",
    "from langchain_text_splitters import CharacterTextSplitter\n",
    "from lancedb.rerankers import LinearCombinationReranker\n",
    "from langchain_community.tools import TavilySearchResults\n",
    "\n",
    "\n",
    "class DocumentManager:\n",
    "    def __init__(\n",
    "        self,\n",
    "        directory_path,\n",
    "        table_name,\n",
    "        embedding_model=\"text-embedding-ada-002\",\n",
    "        reranker_weight=0.3,\n",
    "    ):\n",
    "        self.directory_path = directory_path\n",
    "        self.table_name = table_name\n",
    "        self.embedding_model = embedding_model\n",
    "        self.reranker_weight = reranker_weight\n",
    "        self.embeddings = OpenAIEmbeddings(model=embedding_model)\n",
    "        self.reranker = LinearCombinationReranker(weight=reranker_weight)\n",
    "        self.docsearch = self._initialize_docsearch()\n",
    "        print(f\"Embeddings for {self.table_name} are done. You can now use it.\")\n",
    "\n",
    "    def _initialize_docsearch(self):\n",
    "        loader = PyPDFDirectoryLoader(self.directory_path)\n",
    "        documents = loader.load()\n",
    "        if not documents:\n",
    "            print(f\"No documents found in directory: {self.directory_path}\")\n",
    "            return None\n",
    "        else:\n",
    "            print(f\"Loaded {len(documents)} documents from {self.directory_path}\")\n",
    "\n",
    "        text_splitter = CharacterTextSplitter()\n",
    "        documents = text_splitter.split_documents(documents)\n",
    "        if not documents:\n",
    "            print(\n",
    "                f\"Document splitting resulted in an empty list for directory: {self.directory_path}\"\n",
    "            )\n",
    "            return None\n",
    "\n",
    "        return LanceDB.from_documents(\n",
    "            documents,\n",
    "            self.embeddings,\n",
    "            reranker=self.reranker,\n",
    "            table_name=self.table_name,\n",
    "        )\n",
    "\n",
    "    def get_docsearch(self):\n",
    "        if self.docsearch is None:\n",
    "            raise ValueError(\n",
    "                f\"Document search initialization failed for table: {self.table_name}\"\n",
    "            )\n",
    "        return self.docsearch\n",
    "\n",
    "\n",
    "# Initialize DocumentManagers for different tools\n",
    "guide_manager = DocumentManager(\n",
    "    directory_path=\"/content/export_import_policies/how_to_export\",\n",
    "    table_name=\"guide_export_import\",\n",
    ")\n",
    "export_policy_manager = DocumentManager(\n",
    "    directory_path=\"/content/export_import_policies/export_product_policy\",\n",
    "    table_name=\"export_policy_info\",\n",
    ")\n",
    "import_policy_manager = DocumentManager(\n",
    "    directory_path=\"/content/export_import_policies/import_product_policy\",\n",
    "    table_name=\"import_policy_info\",\n",
    ")\n",
    "\n",
    "\n",
    "# tools\n",
    "@tool\n",
    "def guide_on_how_to_export(query: str):\n",
    "    \"\"\"\n",
    "    Use this tool when the query is about general guidance on how to start an export-import business, documentation requirements, or basic procedures.\n",
    "\n",
    "    \"\"\"\n",
    "    docsearch = guide_manager.get_docsearch()\n",
    "    docs = docsearch.similarity_search_with_relevance_scores(query, k=5)\n",
    "    return [{\"text\": doc.page_content, \"score\": score} for doc, score in docs]\n",
    "\n",
    "\n",
    "@tool\n",
    "def export_policy(query: str):\n",
    "    \"\"\"\n",
    "    Use this tool when the query is related to export policies, regulations, or HS codes for specific products\n",
    "    \"\"\"\n",
    "    docsearch = export_policy_manager.get_docsearch()\n",
    "    docs = docsearch.similarity_search_with_relevance_scores(query, k=5)\n",
    "    return [{\"text\": doc.page_content, \"score\": score} for doc, score in docs]\n",
    "\n",
    "\n",
    "@tool\n",
    "def import_policy(query: str):\n",
    "    \"\"\"\n",
    "    Use this tool when the query is about import regulations, restrictions, and HS codes for any product.\n",
    "    \"\"\"\n",
    "    docsearch = import_policy_manager.get_docsearch()\n",
    "    docs = docsearch.similarity_search_with_relevance_scores(query, k=5)\n",
    "    return [{\"text\": doc.page_content, \"score\": score} for doc, score in docs]\n",
    "\n",
    "\n",
    "@tool\n",
    "def news_related_exim(query: str):\n",
    "    \"\"\"\n",
    "    use this tool when  we need to search latest inforamtion from net such as exporting importing countries .news realted export import .\n",
    "    \"\"\"\n",
    "    tool = TavilySearchResults(\n",
    "        max_results=5,\n",
    "        search_depth=\"advanced\",\n",
    "        include_answer=True,\n",
    "        include_raw_content=True,\n",
    "        include_images=True,\n",
    "    )\n",
    "    news = tool.invoke({\"query\": query})\n",
    "    return news"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "FdAIwNfPhMpa",
    "outputId": "beacee08-1bec-4819-df50-048f39ef0992"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Loaded 94 documents from /content/export_import_policies/how_to_export\n",
      "Embeddings for guide_export_import are done. You can now use it.\n",
      "Loaded 138 documents from /content/export_import_policies/export_product_policy\n",
      "Embeddings for export_policy_info are done. You can now use it.\n",
      "Loaded 32 documents from /content/export_import_policies/import_product_policy\n",
      "Embeddings for import_policy_info are done. You can now use it.\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# Use openai llm . # feel free to use any llm\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langgraph.checkpoint.memory import MemorySaver\n",
    "from langgraph.prebuilt import create_react_agent\n",
    "\n",
    "model = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n",
    "\n",
    "# List of tools\n",
    "tools = [guide_on_how_to_export, export_policy, import_policy, news_related_exim]\n",
    "\n",
    "system_prompt = \"\"\"You are an expert import export assistant bot. use the avaialble tools for asnwering user quetions. only search the realted prdcut on inofmration if not abibale simply return nithing .try to give best answer to user so he wil get all infromatino\"\"\"\n",
    "\n",
    "memory = MemorySaver()\n",
    "\n",
    "# Create the agent\n",
    "graph = create_react_agent(\n",
    "    model, tools=tools, prompt=system_prompt, checkpointer=memory\n",
    ")\n",
    "\n",
    "\n",
    "# Function to print output stream\n",
    "def print_stream(stream):\n",
    "    for s in stream:\n",
    "        message = s[\"messages\"][-1]\n",
    "        if isinstance(message, tuple):\n",
    "            print(message)\n",
    "        else:\n",
    "            message.pretty_print()\n",
    "\n",
    "\n",
    "# Example usage:\n",
    "\n",
    "config = {\"configurable\": {\"thread_id\": \"thread-1\"}}\n",
    "\n",
    "\n",
    "inputs = {\n",
    "    \"messages\": [(\"user\", \"i want to export organic chemicals acyclic aldehyde.\")]\n",
    "}\n",
    "print_stream(graph.stream(inputs, config, stream_mode=\"values\"))"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "qLVgPDjrJd6E",
    "outputId": "849d7765-4541-4640-a9bd-ef282aeb8bd6"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "i want to export organic chemicals acyclic aldehyde.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Tool Calls:\n",
      "  export_policy (call_kbOJUx7kY4wpEvJzjXmit52r)\n",
      " Call ID: call_kbOJUx7kY4wpEvJzjXmit52r\n",
      "  Args:\n",
      "    query: export policies and regulations for organic chemicals acyclic aldehyde\n",
      "  guide_on_how_to_export (call_YSV82DKqjYrjdLFfyLTalJmk)\n",
      " Call ID: call_YSV82DKqjYrjdLFfyLTalJmk\n",
      "  Args:\n",
      "    query: how to export organic chemicals acyclic aldehyde\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: guide_on_how_to_export\n",
      "\n",
      "[{\"text\": \"HOW TO EXPORT  \\n A practical guide \\nMinistry of Commerce & Industry \\nGovernment of India \\nविदेश व्यापार महानिदेशालय \\nDIRECTORATE GENERAL OF  \\nFOREIGN TRADE \\nNIRYAT \\nBANDHU \\nSCHEME \\n(2023-24)\", \"score\": 0.6190895958004871}, {\"text\": \"49 | P a g e  \\n \\n- Gambier \\nExtracts/Myrobalam \\nExtracts/ Cutch Extracts/ \\nOther dyeing & tanning \\nextracts \\n- Fire works \\n- Safety Matches \\n- Activated Carbon \\n- Cocoanut shell Charcoal \\n- Superphosphates Urea \\n- Other Chemical \\nFertilizers \\n- Hard Aggregates for  Floor \\n Ossein, Glue & Gelatine \\n Paints & Allied Products \\n Paper, Board & Paper Products \\n Plywood & Allied Products \\n Processed Minerals \\n Rubber Manufactured Products \\n5 Cotton Textiles Export \\nPromotion Council \\n Cotton Yarn & Sewing Thread \\n Cotton Fabrics (Grey/Bleached & \\nProcessed Fabrics) including Yarn \\nDyed Fabrics: \\n Duck/Canvas, Sheetings, Poplin, \\nShirting/Suitings, Denims/Drills, \\nTwills/Sateens, \\nSarees/Dhotis/Terry Fabrics, \\nFurnishings, Voils/Mulls/Muslin, \\nKnit Fabrics \\n Cotton Made-ups – Bed Linens/ \\nHome Furnishings, Terry Towels/ \\nToweling, Bags/Sacks, Curtains \\n/Drapes, Blankets, Table/Toilet \\n/Kitchen, Linens/Napkins, \\nHandkerchiefs / Dusters, \\nCarpets/Mats/ Tarpaulins/Tents, \\nTapes/Narrow Fabrics, Labels, \\nShawls/Scarves, Rope &* Twine, \\nDrop Cloth, Mosquito \\nNets/Netting, Embroidered \\nFabrics/Sarees, Dress Materials, \\nChaddar/Odhanis, Khangas, \\nThreads/Packing Threads, Others \\n     Raw Cotton \\n6 Council for Leather Exports Leather & Leather products \\n7 EEPC INDIA \\n(Formerly Engineering \\nExport Promotion Council) \\n Machineries and equipments \\n Motor Vehicles \\n Automobile Components \\n Bicycles,  Bicycle Components and \\nAccessories\", \"score\": 0.6111237992850795}, {\"text\": \"54 | P a g e  \\n \\n34 Agricultural and  \\nProcessed Food  \\nProducts Export  \\nDevelopment \\nAuthority (APEDA)  \\n Fruits, Vegetable and their \\nproducts \\n Meat and meat products  \\n Poultry and poultry products \\n Dairy products \\n Confectionary, biscuits and bakery \\nproducts \\n Honey, jiggery and sugar products \\n Cocoa and its products, chocolates \\nof all kinds \\n Alcoholic and non-alcoholic \\nbeverages \\n Cereals and cereals products \\n Groundnuts, peanuts and walnut \\n Pickles, chutneys and papads \\n Guar Gum \\n Floriculture and floriculture \\nproducts \\n Herbal and medicinal plants \\n Cashew Kernels \\n Cashew Shell Liquid \\n Kardanoliv \\n35 Coconut Development  \\nBoard \\nAll coconut products other than those made \\nfrom coconut husk & fiber \\n36 Marine Products  \\nExport Development  \\nAuthority \\n(MPEDA) \\nMarine Products including all varieties  of \\nfishery products known commercially  as \\nshrimp, prawn,  lobster, crab, fish, shell -\\nfish, other aquatic animals or plants or part \\nthereof. \\n \\nRole of EPCs: \\n  Export Promotion Councils encourage exporters, both old and new, to attend \\na variety of workshops that they conduct throughout the year. These workshops \\naddress topics like documentation, understanding of commercial terms, designing \\nbusiness plans, and understanding finances. These are excellent platforms for \\nexporters to find solutions and advice on their business and financial needs. One -to-\\none correspondence with the EPCs to seek financial guidance is also an option. \\n3. ECGC Limited:  \\n      ECGC Ltd. (Formerly known as Export Credit Guarantee Corporation of India \\nLtd.) wholly owned by Government of India, was set up in 1957 with the objective of \\npromoting e xports from the country by providing credit risk insurance and related \\nservices for exports.\", \"score\": 0.6082336212952936}, {\"text\": \"73 | P a g e  \\n \\nAdvance Authorization For Annual Requirement: \\n \\nExporters having past export performance (in at least preceding two financial \\nyears) shall be entitled for Advance Authorization for Annual requirement. This shall \\nonly be issued for items having SION. \\n \\nDuty Free Import Authorization (DFIA) Scheme: \\n \\nDFIA is issued to allow duty free import of inputs, with a minimum value addition \\nrequirement of 20%. DFIA shall be exempted only from the payment of basic customs \\nduty. DFIA shall be issued on post export basis for products for which SION has been \\nnotified. Separate schemes exist for gems and jewellery sector for which FTP may be \\nreferred. \\n \\nDuty Drawback of Customs: \\n \\nThe scheme is administered by Department of Revenue. Under this scheme \\nproducts made out of duty paid inputs are first exported and thereafter refund of duty \\nis claimed in two ways: \\n \\ni) All Industry Rates:   As per Schedule \\nii) Brand Rate          :   As per application on the basis of data/documents \\n \\nInterest Equalisation Scheme (IES): \\n \\nThe Government announced the Interest Equalisation Scheme @ 3% per annum \\nfor Pre and Post Shipment Rupee Export Credit with effect from 1st April, 2015 for 5 \\nyears available to all exports under 416 tariff lines [at ITC (HS) code of 4 digit] and \\nexports ma de by Micro, Small & Medium Enterprises (MSMEs) across all ITC(HS) \\ncodes. With effect from November 2, 2018, the rate of Interest Equalisation for MSME \\nhas been increased to 5%. The Scheme has also been extended to Merchant Exporters \\nwho will now avail the  benefit @ 3% for all exports under 416 tariff lines w.e.f.     \\nJanuary 2, 2019. \\n \\nEXPORT PROMOTION CAPITAL GOODS SCHEMES (EPCG)  \\n \\nZero Duty EPCG Scheme: \\n \\nUnder this scheme import of capital goods at zero custom duty is allowed for \\nproducing quality goods and services to enhance India’s export competitiveness. \\nImport under EPCG shall be subject to export obligation equivalent to six times of duty \\nsaved in six years. Scheme also allows indigenous sourcing of capital goods with 25% \\nless export obligation.\", \"score\": 0.6077713546608261}, {\"text\": \"77 | P a g e  \\n \\nIndigenous Sourcing of Capital Goods and benefits to Domestic Supplier: \\nA person holding an EPCG Authorisation may source capital goods from a \\ndomestic manufacturer either through Invalidation Letter or through Advance Release \\nOrder. Such domestic manufacturer shall be eligible for deemed export benefits under \\nparagraph 7.03 o f FTP, and as may be provided under GST Rules under the category \\nof deemed exports. Such domestic sourcing shall also be permitted from EOUs and \\nthese supplies shall be counted for purpose of fulfilment of positive NFE by said EOU \\nas provided in Para 6.08 (a) of FTP. \\n \\nADVANCE AUTHORIZATION (AA) SCHEME \\nIntroduction:  \\n  Advance Authorisation is issued to allow duty free import of input, which is \\nphysically incorporated in export product (making normal allowance for wastage). In \\naddition, fuel, oil, catalyst which is consumed / utilized in the process of production of \\nexport product, may also be allowed  Under Advance Authorisation scheme export \\nobligation is to be fulfilled by exporting finished products manufactured by imported \\ninputs with minimum value addition as mentioned below  \\nMinimum Value Addition: \\n(i) Minimum value addition required to be achieved under Advance Authorisation is \\n15%.  \\n(ii) Export Products where value addition could be less than 15% are given in \\nAppendix 4D.  \\n(iii) Minimum value addition for Gems & Jewellery Sector is given in paragraph 4.60 \\nof Handbook of Procedures.  \\n(iv)  In case of Tea, minimum value addition shall be 50%.  \\n(iv) In case of spices, minimum value addition shall be 25%. \\nDomestic Sourcing of Inputs  \\n Holder of an Advance Authorisation / Duty Free Import Authorisation can \\nprocure inputs from indigenous supplier/ State Trading \\nEnterprise/EOU/EHTP/BTP/ STP in lieu of direct import. Such \\nprocurement can be against Advance Release Order (ARO), or Invalidation \\nLetter.  \\n When domestic supplier intends to obtain duty free material for inputs \\nthrough Advance Authorisation for supplying resultant product to another \\nAdvance Authorisation / DFIA /EPCG Authorisation, Regional Authority \\nshall issue Invalidation Letter.  \\n Regional Authority shall issue Advance Release Order if the domestic \\nsupplier intends to seek refund of duties exempted through Deemed \\nExports mechanism as per provisions under Chapter-7 of FTP.  \\n Regional Authority may issue Advance Release Order or Invalida tion Letter \\nat the time of issue of Authorisation simultaneously or subsequently.  \\n Advance Authorisation holder under DTA can procure inputs from / SEZ \\nunits against Certificate of supply till EDI message system between SEZ \\nand Customs is enabled.\", \"score\": 0.6046244602167601}]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "To export organic chemicals, specifically acyclic aldehyde, here are the relevant details:\n",
      "\n",
      "### Export Policies and Regulations:\n",
      "1. **HS Code and Description**: \n",
      "   - Acyclic aldehydes fall under the category of organic chemicals. The specific HS code for acyclic aldehydes is not directly mentioned, but they are generally classified under the broader category of aldehydes.\n",
      "   - The export policy for acyclic aldehydes is generally free, meaning there are no specific restrictions or prohibitions on their export.\n",
      "\n",
      "2. **General Export Conditions**:\n",
      "   - Ensure compliance with any international agreements or protocols, such as the Montreal Protocol, if applicable.\n",
      "   - Obtain any necessary certifications or authorizations if the chemicals are subject to specific international regulations.\n",
      "\n",
      "### Guide on How to Export:\n",
      "1. **Documentation**:\n",
      "   - Obtain an Export License if required.\n",
      "   - Prepare necessary shipping documents such as the commercial invoice, packing list, and bill of lading.\n",
      "   - Ensure compliance with the destination country's import regulations.\n",
      "\n",
      "2. **Export Procedures**:\n",
      "   - Register with the Directorate General of Foreign Trade (DGFT) in your country.\n",
      "   - Identify potential markets and buyers for your product.\n",
      "   - Arrange for logistics and transportation, ensuring that the chemicals are packaged and labeled according to international standards.\n",
      "\n",
      "3. **Financial and Legal Considerations**:\n",
      "   - Consider insurance for the shipment to cover any potential risks.\n",
      "   - Understand the financial implications, including currency exchange rates and payment terms.\n",
      "\n",
      "4. **Export Promotion Schemes**:\n",
      "   - Explore any available export promotion schemes or incentives provided by the government to support exporters.\n",
      "\n",
      "By following these guidelines and ensuring compliance with relevant regulations, you can successfully export acyclic aldehydes. If you need more specific information or assistance, consulting with a trade expert or export promotion council may be beneficial.\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "We can see that memory is being used here. A follow-up question was asked, and it worked as expected, maintaining context."
   ],
   "metadata": {
    "id": "YHDGCsGpvN1c"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "config = {\"configurable\": {\"thread_id\": \"thread-1\"}}\n",
    "\n",
    "inputs = {\"messages\": [(\"user\", \"what is HS code of it & export policy .\")]}\n",
    "print_stream(graph.stream(inputs, config, stream_mode=\"values\"))"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "kXzieoD_hMt6",
    "outputId": "cd13c6fc-e461-44d9-c38a-b8a7341020e3"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "what is HS code of it & export policy .\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Tool Calls:\n",
      "  export_policy (call_2uvO8ql0VfDUeSdjrmN60qAH)\n",
      " Call ID: call_2uvO8ql0VfDUeSdjrmN60qAH\n",
      "  Args:\n",
      "    query: HS code and export policy for acyclic aldehyde\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: export_policy\n",
      "\n",
      "[{\"text\": \"Product Description and Export Policy\\nItc(hs) Code. Description Export\\nPolicy Policy Condition\\n2901 Acyclic hydrocarbons.\\n29011000 Saturated Free\\n29012100 Unsaturated : -- Ethylene Free\\n29012200 Unsaturated : -- Propene (propylene) Free\\n29012300 Unsaturated : -- Butene (butylene) and isomers thereof Free\\n29012400 Unsaturated : -- Buta-1,3-diene and isoprene Free\\n290129 Unsaturated : -- Other\\n29012910 Acetylene, whether or not in dissolved condition Free\\n29012920 Heptene (Heptylene) Free\\n29012930 Dihydromyrcene Free\\n29012940 Tetradecene Free\\n29012990 Other Free\\n2902 Cyclic hydrocarbons.\\n29021100 Cyclanes, cyclenes and cycloterpenes : -- Cyclohexane Free\\n290219 Cyclanes, cyclenes and cycloterpenes : -- Other\\n29021910 Cyclopropyl actetylene Free\", \"score\": 0.7862199732910415}, {\"text\": \"Product Description and Export Policy\\nItc(hs) Code. Description Export\\nPolicy Policy Condition\\n0801\\nCoconuts, Brazil nuts\\nand cashew nuts, fresh\\nor dried, whether or not\\nshelled or peeled.\\n08011100 Coconuts : --\\nDesiccated Free\\n080112 Coconuts : -- In the\\ninner shell (endocarp)\\n08011210 Fresh Free\\n08011220 Dried Free\\n08011290 Other Free\\n080119 Coconuts : -- Other\\n08011910 Fresh Free\\n08011920 Dried Free\\n08011990 Other Free\\n08012100 Brazil nuts : -- In shell Free\\n08012200 Brazil nuts : -- Shelled Free\\n08013100 Cashew nuts : -- In shell Free\", \"score\": 0.7235636675046249}, {\"text\": \"Product Description and Export Policy\\nItc(hs) Code. Description Export\\nPolicy Policy Condition\\n1701\\nCane or beet sugar\\nand chemically pure\\nsucrose, in solid form.\\n17011200\\nRaw sugar not\\ncontaining added\\nflavouring or colouring\\nmatter : -- Beet sugar\\nFree\\n170113\\nRaw sugar not\\ncontaining added\\nflavouring or colouring\\nmatter : -- Cane sugar\\nspecified in\\nSubheading Note 2 to\\nthis Chapter\\n17011310 Cane jaggery Free\\n17011320 Khandsari sugar Free\\n17011390 Other Free\\n170114\\nRaw sugar not\\ncontaining added\\nflavouring or colouring\\nmatter : -- Other cane\\nsugar\\n17011410 Cane jaggery Free\", \"score\": 0.7228998335671539}, {\"text\": \"Product Description and Export Policy\\nItc(hs) Code. Description Export\\nPolicy Policy Condition\\n0901\\nCoffee, whether or not roasted or\\ndecaffeinated; coffee husks and skins;\\ncoffee substitutes containing coffee in any\\nproportion.\\n090111 Coffee, not roasted : --Not decaffeinated\\n09011111 Arabica plantation: ---- A Grade Free\\n09011112 Arabica plantation: ---- B Grade Free\\n09011113 Arabica plantation: ---- C Grade Free\\n09011119 Arabica plantation: ---- Other Free\\n09011121 Arabica Cherry: ---- AB Grade Free\\n09011122 Arabica Cherry: ---- PB Grade Free\\n09011123 Arabica Cherry: ---- C Grade Free\\n09011124 Arabica Cherry: ---- B/B/B Grade Free\\n09011129 Arabica Cherry: ---- Other Free\\n09011131 Rob Parchment: ---- AB Grade Free\\n09011132 Rob Parchment: ---- PB Grade Free\\n09011133 Rob Parchment: ---- C Grade Free\", \"score\": 0.7203694210762569}, {\"text\": \"permitted under\\nRestricted Export\\nAuthorization.\\n293499 Other -- Other\\n29349910 Chloro thiophene-2-carboxylic acid Free\\nHowever, Export of\\n\\\"Deoxy nucleotide\\ntriphosphates\\\" will be\\nin Restricted\\ncategory and\\npermitted under\\nRestricted Export\\nAuthorization.\\n56/2015-20 24.02.20\\n22\\n29349920 Morpholine Free\\nHowever, Export of\\n\\\"Deoxy nucleotide\\ntriphosphates\\\" will be\\nin Restricted\\ncategory and\\npermitted under\\nRestricted Export\\nAuthorization.\\n56/2015-20 24.02.20\\n22\\n29349930 Buprofezin (ISO) Free\\nHowever, Export of\\n\\\"Deoxy nucleotide\\ntriphosphates\\\" will be\\nin Restricted\\ncategory and\\npermitted under\\nRestricted Export\\nAuthorization.\\n56/2015-20 24.02.20\\n22\\n29349990 Other Free\\nHowever, Export of\\n\\\"Deoxy nucleotide\\ntriphosphates\\\" will be\\nin Restricted\\n56/2015-20 24.02.20\\n22\", \"score\": 0.7145604895333945}]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "The HS code for acyclic aldehydes falls under the broader category of acyclic hydrocarbons, which is 2901. However, for specific acyclic aldehydes, you might need to refer to more detailed sub-categories or consult with a trade expert for precise classification.\n",
      "\n",
      "### Export Policy:\n",
      "- **Export Policy**: The export of acyclic aldehydes is generally free, meaning there are no specific restrictions or prohibitions on their export.\n",
      "\n",
      "If you need more detailed classification or specific sub-codes, it might be helpful to consult with a customs expert or use a detailed HS code directory.\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "This is how you can leverage multiple sources, integrate them, and build a multi-source RAG agent."
   ],
   "metadata": {
    "id": "RCB_62zXzVEC"
   }
  }
 ]
}